<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教练" prop="coachName">
        <el-autocomplete class="inline-input" value-key="coachName"
          v-model="queryParams.coachName"
          :fetch-suggestions="queryCoachName"
          placeholder="请输入学员姓名"
          @select="handleQueryCoachName"></el-autocomplete>
      </el-form-item>
      <el-form-item label="月份" prop="salaryMonth">
        <el-input
          v-model="queryParams.salaryMonth"
          placeholder="请输入月份,如202405"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发放日期" prop="payTime">
        <el-date-picker clearable
          v-model="queryParams.payTime"
          type="date"
          value-format="yyyy.MM.dd"
          placeholder="请选择发放日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['puyu:coachsalary:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['puyu:coachsalary:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['puyu:coachsalary:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['puyu:coachsalary:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="coachsalaryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="教练薪资ID" align="center" prop="coachSalaryId" />
      <el-table-column label="教练姓名" align="center" prop="coachName" />
      <el-table-column label="入职时间" align="center" >
        <template slot-scope="scope">
          <span>{{ parseTime(getJoinTime(scope.row.coachId),'{y}.{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="底薪" align="center" prop="salaryBasic" >
        <template slot-scope="scope">
          {{ scope.row.salaryBasic.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="课时费" align="center" prop="classFee" >
        <template slot-scope="scope">
          {{ scope.row.classFee.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="课时数" align="center" prop="lessonNum" />
      <el-table-column label="补贴" align="center" prop="subsidy" >
        <template slot-scope="scope">
          {{ scope.row.subsidy.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="发放金额" align="center" prop="salarySum" >
        <template slot-scope="scope">
          {{ scope.row.salarySum.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="发放日期" align="center" prop="payTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['puyu:coachsalary:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['puyu:coachsalary:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教练薪资对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="工资月份" prop="salaryMonth">
          <!-- <el-input v-model="form.salaryMonth" placeholder="请输入月份,如:202405" /> -->
          <el-date-picker
            v-model="form.salaryMonth"
            type="month"
            placeholder="选择月份"
            format="yyyy-MM"
            value-format="yyyyMM">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="教练" prop="coachName">
          <el-autocomplete class="inline-input" value-key="coachName"
            v-model="form.coachName"
            :fetch-suggestions="queryCoachName"
            placeholder="请输入教练姓名"
            @select="handleCoachName"></el-autocomplete>
        </el-form-item>
        <el-form-item label="补贴" prop="subsidy">
          <el-input v-model="form.subsidy" placeholder="请输入补贴,可以是负数,表示扣除工资或绩效" />
        </el-form-item>
        <el-form-item label="发放日期" prop="payTime">
          <el-date-picker clearable
            v-model="form.payTime"
            type="date"
            value-format="yyyy.MM.dd"
            placeholder="请选择发放日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCoachsalary, getCoachsalary, delCoachsalary, addCoachsalary, updateCoachsalary } from "@/api/puyu/coachsalary";
import { listCoachs } from "@/api/puyu/coach";

export default {
  name: "Coachsalary",
  dicts: ['sys_del_flag'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教练薪资表格数据
      coachsalaryList: [],
      // 教练数据
      coachList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        userId: null,
        coachId: null,
        coachName: null,
        salaryMonth: null,
        salaryBasic: null,
        classFee: null,
        lessonNu: null,
        subsidy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        coachName: [
          { required: true, message: "教练姓名不能为空", trigger: "change" }
        ],
        salaryMonth: [
          { required: true, message: "月份不能为空", trigger: "blur" },
          { pattern: /(^[1-9]\d{5}$)/, message: '月份必须介于 190001 和 999999 之间，如：202405', trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
    // 获取教练下拉列表
    this.getCoachList();
  },
  methods: {
    /** 查询教练薪资列表 */
    getList() {
      this.loading = true;
      listCoachsalary(this.queryParams).then(response => {
        this.coachsalaryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询教练用户列表 */
    getCoachList() {
      listCoachs().then(response => {
        this.coachList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        coachSalaryId: null,
        deptId: null,
        userId: null,
        coachId: null,
        coachName: null,
        salaryMonth: null,
        salaryBasic: null,
        classFee: null,
        lessonNu: null,
        subsidy: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.coachId = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.coachSalaryId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教练薪资";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const coachSalaryId = row.coachSalaryId || this.ids
      getCoachsalary(coachSalaryId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教练薪资";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.coachSalaryId != null) {
            updateCoachsalary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCoachsalary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const coachSalaryIds = row.coachSalaryId || this.ids;
      this.$modal.confirm('是否确认删除教练薪资编号为"' + coachSalaryIds + '"的数据项？').then(function() {
        return delCoachsalary(coachSalaryIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('puyu/coachsalary/export', {
        ...this.queryParams
      }, `coachsalary_${new Date().getTime()}.xlsx`)
    },
    queryCoachName(queryString, cb) {
        var coachList = this.coachList;
        var results = queryString
            ? coachList.filter(this.filterCoachName(queryString))
            : coachList;
        // 调用 callback 返回建议列表的数据
        cb(results);
    },
    filterCoachName(queryString) {
        return (coach) => {
            return (
                coach.coachName
                    .toLowerCase()
                    .indexOf(queryString.toLowerCase()) > -1
            );
        };
    },
    //选中教练
    handleCoachName(item) {
        this.form.userId = item.userId;
        this.form.deptId = item.deptId;
        this.form.coachId = item.coachId;
        this.form.coachName = item.coachName;
    },
    //选中教练
    handleQueryCoachName(item) {
        this.queryParams.coachId = item.coachId;
    },
    // 根据coachId获取教练入职时间
    getJoinTime(coachId) {
      if (!this.coachList) {
        return '';
      }
      return this.coachList.filter(item => item.coachId == coachId)[0].joinTime;
    },
  }
};
</script>
